package org.jeecg.modules.demo.warehouseorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.qualityOrder.entity.PuQualityOrder;
import org.jeecg.modules.demo.qualityOrder.mapper.PuQualityOrderMapper;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.jeecg.modules.demo.warehouseorder.service.IWarehouseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: warehouse_order
 * @Author: jeecg-boot
 * @Date:   2021-12-02
 * @Version: V1.0
 */
@Service
public class WarehouseOrderServiceImpl extends ServiceImpl<WarehouseOrderMapper, WarehouseOrder> implements IWarehouseOrderService {
    @Resource
    private WarehouseOrderMapper warehouseOrderMapper;
    @Resource
    private PuQualityOrderMapper puQualityOrderMapper;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static final String TITLE = "单据审核";


    private String getMessage(String name,String date) {
        String str ="由【"+name+"】于【"+date+"】发起的采购入库单需要您的审核，请及时查看！";
        return str;
    }


    @Override
    public List<String> selectByPoOrder(String poOrder){
        return warehouseOrderMapper.selectByPoOrder(poOrder);
    }


    @Override
    public Map<String,Double> getRkNumAndTotail(){
        return warehouseOrderMapper.getRkNumAndTotail();
    }

    @Override
    public Map<String,Double> getCkNumAndTotail(){
        return warehouseOrderMapper.getCkNumAndTotail();
    }

    @Override
    public String getMaxCode(){
        return warehouseOrderMapper.getMaxCode();
    }

    @Override
    public WarehouseOrder getByWoNo(String wono){
        return warehouseOrderMapper.getByWoNo(wono);
    }

    @Override
    public Map<String,String>  getMapByCon(String con){
        return warehouseOrderMapper.getMapByCon(con);
    }

    @Override
    public WarehouseOrder getByRkNo(String rkNo) {
        return warehouseOrderMapper.getByRkNo(rkNo);
    }

    @Override
    public  String  getCode(String number){
        return warehouseOrderMapper.getCode(number);
    }
    @Override
    public Map<String,Double> getYkByOrder(String str){
        return warehouseOrderMapper.getYkByOrder(str);
    }

    @Override
    @Transactional
    public boolean waiveChecKWarehouseOrder(WarehouseOrder warehouseOrder){
        try{
            warehouseOrder.setShTime(null);
            warehouseOrder.setShenhe(0);
            warehouseOrderMapper.updateById(warehouseOrder);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    @Transactional
    public boolean checkWarehouseOrder(WarehouseOrder warehouseOrder){
        try{
            warehouseOrder.setFpje(new BigDecimal(0.000));
            warehouseOrder.setFpsl(new BigDecimal(0.000));
            warehouseOrder.setFpdj(new BigDecimal(0.000));
            warehouseOrder.setZgsl(warehouseOrder.getQoQuantity());
            warehouseOrder.setZxcb(warehouseOrder.getWsje());
            warehouseOrder.setShenhe(1);
            warehouseOrder.setShTime(new Date());
            warehouseOrderMapper.updateById(warehouseOrder);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    @Transactional
    public boolean  addRkOrder(WarehouseOrder warehouseOrder){
        try{
            warehouseOrder.setId(null);
            warehouseOrder.setCreateTime(new Date());
            warehouseOrder.setShenhe(0);
            warehouseOrder.setShanchu(0);
            warehouseOrder.setFeiyong(0);
            warehouseOrder.setWsdj(warehouseOrder.getFtwsdj());
            warehouseOrder.setWsje(warehouseOrder.getFtwsje());
            warehouseOrder.setHsdj(warehouseOrder.getFthsdj());
            warehouseOrder.setHsje(warehouseOrder.getFthsje());
            warehouseOrder.setTotalcost(warehouseOrder.getFtwsje());
            warehouseOrder.setStandingstock(warehouseOrder.getQoQuantity());
            warehouseOrder.setFttotalcost(warehouseOrder.getFtwsje());
            warehouseOrder.setPsTax(warehouseOrder.getFtse());
            warehouseOrder.setMeasureunit(warehouseOrder.getMeasureunit());
            warehouseOrder.setSupsttelemnt(warehouseOrder.getQoQuantity());
            warehouseOrder.setFyje(new BigDecimal(0.000));
            warehouseOrder.setShTime(null);
            warehouseOrder.setIsSettlement(warehouseOrder.getRklb()==6?3:0);
            if(warehouseOrder.getIsMerge()==1){
                String[] strings = warehouseOrder.getQoNum().split(",");
                for (String string : strings) {
                    PuQualityOrder puQualityOrder = puQualityOrderMapper.getByQoNum(string);
                    puQualityOrder.setRuku(1);
                    puQualityOrderMapper.updateById(puQualityOrder);
                }
            }else if(warehouseOrder.getQoNum()!=null){
                PuQualityOrder puQualityOrder = puQualityOrderMapper.getByQoNum(warehouseOrder.getQoNum());
                puQualityOrder.setRuku(1);
                puQualityOrderMapper.updateById(puQualityOrder);
            }
            warehouseOrderMapper.insert(warehouseOrder);


            LoginUser user = sysBaseAPI.getUserById(warehouseOrder.getPoStaff());
            TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
            templateMessageDTO.setFromUser("管理员");
            templateMessageDTO.setToUser(warehouseOrder.getSaleman()+",");
            templateMessageDTO.setTitle(TITLE);
            templateMessageDTO.setTemplateCode("order");
            Map<String, String> templateParam = new HashMap<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date= simpleDateFormat.format(new Date());
            templateParam.put("ts_date",date);
            templateParam.put("ts_content", getMessage(user!=null?user.getRealname():"业务员不存在！", date));
            templateParam.put("checkOrder",warehouseOrder.getWoNo());
            templateParam.put("cd_url","/pubuyorderToRk/puBuyOrderToRk/list");
            templateMessageDTO.setTemplateParam(templateParam);
            sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteByRkId(WarehouseOrder warehouseOrder){
        try{
            if(warehouseOrder.getIsMerge()==1){
                String[] strings = warehouseOrder.getQoNum().split(",");
                for (String string : strings) {
                    PuQualityOrder puQualityOrder = puQualityOrderMapper.getByQoNum(string);
                    puQualityOrder.setRuku(0);
                    puQualityOrderMapper.updateById(puQualityOrder);
                }
            }else if(warehouseOrder.getQoNum()!=null){
                    PuQualityOrder puQualityOrder = puQualityOrderMapper.getByQoNum(warehouseOrder.getQoNum());
                    puQualityOrder.setRuku(0);
                    puQualityOrderMapper.updateById(puQualityOrder);
            }
            warehouseOrderMapper.deleteById(warehouseOrder.getId());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

}
